package com.socialbank.web.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nybank.fastdfs.FastDFSFile;
import com.nybank.fastdfs.FileManager;
import com.socialbank.web.FinanceBean.BankMetal;
import com.socialbank.web.mapper.BankInsuranceMapper;
import com.socialbank.web.mapper.BankMetalMapper;
import com.socialbank.web.service.BankMetalService;
import com.wqdata.auth.util.PageParam;
import com.wqdata.auth.util.UserUtils;
@Service
public class BankMetalServiceImpl implements BankMetalService{
	@Autowired
	private SqlSession sqlSession;
	@Override
	public void save(MultipartFile file, BankMetal bankMetal) {
		// TODO Auto-generated method stub
		BankMetalMapper mapper = sqlSession.getMapper(BankMetalMapper.class);
		String fileAbsolutePath="";//上传文件地址

		String groupName="";//文件服务器组名
		String remoteFileName="";//最终保存的文件名
		String fileType="";//文件类型
		if(file!=null&&!file.isEmpty()){
			int beginIndex=file.getOriginalFilename().indexOf(".")+1;
			fileType=file.getOriginalFilename().substring(beginIndex);
			Date date=new Date();
			SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
			//以时间命名
			Map<String,Object> map=new HashMap<String,Object>();
			try{
				FastDFSFile dfsFile = new FastDFSFile(sdf.format(date).toString(), file.getBytes(), fileType);
				map = FileManager.upload(dfsFile);
			}catch(Exception e){
				e.printStackTrace();
			}
			System.out.println(fileAbsolutePath);
			fileAbsolutePath=(String)map.get("fileAbsolutePath");
			groupName=(String)map.get("groupName");
			remoteFileName=(String)map.get("remoteFileName");
			bankMetal.setPicturePath(fileAbsolutePath);
			bankMetal.setGroupName(groupName);
			bankMetal.setRemoteFileName(remoteFileName);
		}
		bankMetal.setDeptId(UserUtils.getCurrrentDeptId());
		mapper.save(bankMetal);

	}

	@Override
	public void deleteById(String id) {
		// TODO Auto-generated method stub
		BankMetalMapper mapper = sqlSession.getMapper(BankMetalMapper.class);
		BankMetal bankMetal=new BankMetal();
		bankMetal=mapper.findById(id);
		String picture_path=bankMetal.getPicturePath();
		if(picture_path!=null&&!"".equals(picture_path)){
			String groupName=bankMetal.getGroupName();
			String remoteFileName=bankMetal.getRemoteFileName();
			try {
				FileManager.deleteFile(groupName, remoteFileName);//删除文件服务器上的文件
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		mapper.deleteById(id);
	}

	@Override
	public PageInfo<BankMetal> getList(PageParam pageParam,BankMetal bankMetal) {
		// TODO Auto-generated method stub
		BankMetalMapper mapper = sqlSession.getMapper(BankMetalMapper.class);
		PageHelper.startPage(pageParam.getPageNo(), pageParam.getPageSize());
		bankMetal.setDeptId(UserUtils.getCurrrentDeptId());
		List<BankMetal> manages=mapper.getList(bankMetal);
		PageInfo<BankMetal> pageInfo = new PageInfo<>(manages);
		return pageInfo;
	}

	@Override
	public BankMetal findById(String id) {
		// TODO Auto-generated method stub
		BankMetalMapper mapper = sqlSession.getMapper(BankMetalMapper.class);
		BankMetal bankMetal=new BankMetal();
		bankMetal=mapper.findById(id);
		return bankMetal;
	}

	@Override
	public void updateById(MultipartFile file, BankMetal bankMetal) {
		// TODO Auto-generated method stub
		BankMetalMapper mapper = sqlSession.getMapper(BankMetalMapper.class);
		String fileAbsolutePath="";//上传文件地址
		String groupName="";//文件服务器组名
		String remoteFileName="";//最终保存的文件名
		String fileType="";//文件类型
		if(file!=null&&!file.isEmpty()){
			int beginIndex=file.getOriginalFilename().indexOf(".")+1;
			fileType=file.getOriginalFilename().substring(beginIndex);
				Date date=new Date();
				SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
				//以时间命名
				Map<String,Object> map=new HashMap<String,Object>();
				try {
				FastDFSFile dfsFile = new FastDFSFile(sdf.format(date).toString(), file.getBytes(), fileType);
				map = FileManager.upload(dfsFile);
				}catch(Exception e){
					e.printStackTrace();
				}
				System.out.println(fileAbsolutePath);
				fileAbsolutePath=(String)map.get("fileAbsolutePath");
				groupName=(String)map.get("groupName");
				remoteFileName=(String)map.get("remoteFileName");
				bankMetal.setPicturePath(fileAbsolutePath);
				bankMetal.setGroupName(groupName);
				bankMetal.setRemoteFileName(remoteFileName);
		}else{//如果没有上传图片，则不修改
			String id=bankMetal.getId();
			BankMetal metal=new BankMetal();
			metal=mapper.findById(id);
			bankMetal.setPicturePath(metal.getPicturePath());
			bankMetal.setGroupName(metal.getGroupName());
			bankMetal.setRemoteFileName(metal.getRemoteFileName());
		}


		mapper.updateById(bankMetal);
	}

	@Override
	public List<BankMetal> getListAll(int deptId) {
		// TODO Auto-generated method stub
		BankMetalMapper mapper = sqlSession.getMapper(BankMetalMapper.class);
		List<BankMetal> list=new ArrayList<BankMetal>();
		list=mapper.getListAll(deptId);
		return list;
	}

}
